<?php

namespace App\HttpController\Api;

use App\Think\Db;

/**
 * 公告消息接口Notice
 */
class Notice extends ApiBase
{
    protected $noNeedLogin = ['announce_list'];

    /**
     * 公告列表
     * @param string $type 类型:home=首页公告,system=系统公告
     * @param string $page 当前页数
     */
    public function announce_list(){
        $type = $this->input("type");
        $page = $this->input("page");
        if(!$page || !$type) return $this->error(__('请求参数不完整'));
        $where = "";
        if($type == 'home') $where .= " AND ishome=1 ";
        $data = Db::name('announce')->field('title,content,createtime')->where("status=1 ".$where)->page("{$page},20")->order("istop DESC,id DESC")->select(); 
        return $this->success(__('success'), $data);
    }

    /**
     * 通知列表
     * @param string $page 当前页数
     */
    public function notice_list(){
        $page = $this->input("page");
        if(!$page) return $this->error(__('请求参数不完整'));

        $where = "user_id='{$this->auth->id}'";
        $data = Db::name('notice')->where($where)->page("{$page},20")->order('id DESC')->select(); 
        $unread_count = Db::name('notice')->where("{$where} AND isread=0")->count();
        return $this->success(__('success'), ['list'=>$data, 'unread_count'=>$unread_count]);
    }
    /**
     * 阅读通知
     * @param string $id 通知ID|多个用英文,分割
     */
    public function notice_read(){
        $id = $this->input("id");
        if(!$id) return $this->error(__('请求参数不完整'));
        Db::name('notice')->where("user_id='{$this->auth->id}' AND ".db_where_in('id', $id))->update(['isread'=>1]);
        return $this->success(__('success'));
    }
    /**
     * 删除通知
     * @param string $ids 通知ID|多个用英文,分割
     */
    public function notice_del(){
        $ids = $this->input("ids");
        if(!$ids) return $this->error(__('请求参数不完整'));
        Db::name('notice')->where("user_id='{$this->auth->id}' AND ".db_where_in('id', $ids))->delete();
        return $this->success(__('success'));
    }

    /**
     * 意见反馈
     * ApiMethod (POST)
     * @param string $type  反馈类型:1=充值问题,2=账号问题,3=游戏问题,4=活动问题,5=其他问题
     * @param string $title 标题
     * @param string $content 内容
     */
    public function feedback_send(){
        $type    = $this->input("type");
        $title   = $this->input("title");
        $content = $this->input("content");
        if(!$type || !$title || !$content) return $this->error(__('请求参数不完整'));

        $data = [
            'user_id'=>$this->auth->id,
            'type'=>$type,
            'title'=>$title,
            'content'=>$content,
            'createtime'=>time()
        ];
        if(!Db::name('feedback')->insert($data)) return $this->error(__('留言失败,请稍后再试'));
        return $this->success(__('感谢您宝贵的建议!'));
    }

}
